<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Data.mysql.php');

Class FormData extends db
{
	function FormData()
	{
		parent::db();
		
		$this->tableName = 'form';
		
		$this->AddMap("ID", "id", "id", DATATYPE_INTEGER, true);
		$this->AddMap("Name", "name", "name", DATATYPE_TEXT, true);
		$this->AddMap("Title", "title", "title", DATATYPE_TEXT, false);
		$this->AddMap("Submit", "submit", "submit", DATATYPE_TEXT, false);
		$this->AddMap("Reset", "reset", "reset", DATATYPE_TEXT, false);
		$this->AddMap("VerificationFunction", "verificationMethod", "verificationMethod", DATATYPE_TEXT, false);
	}
  
	function Retrieve(&$form)
	{
		$sql  = "SELECT ";
		$sql .= $this->GetFieldList();
		$sql .= "FROM " . $this->GetTableName();
		$sql .= $this->GetWhereClause($form);

		$form->Collection = array();

		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$form->Collection[] = $this->FillRow($row);
		}
	}
  
	function FillRow($row)
	{
		$form = new FormModel();
		
		parent::FillRow(&$form, &$row);
		
		return $form;
	}
	
	function GetFieldList()
	{
		parent::GetFieldList(&$select);
		
		return $select;
	}
}

Class FormFieldData extends db
{
	function FormFieldData()
	{
		parent::db();
		
		$this->tableName = 'formfield';

		$this->AddMap("ID", "id", "id", DATATYPE_INTEGER, true);
		$this->AddMap("Label", "label", "label", DATATYPE_TEXT, false);
		$this->AddMap("Type", "type", "type", DATATYPE_ENUM, false);
		$this->AddMap("DefaultValue", "defaultValue", "defaultValue", DATATYPE_TEXT, false);
		$this->AddMap("Length", "length", "length", DATATYPE_INTEGER, false);
		$this->AddMap("Required", "required", "required", DATATYPE_INTEGER, false);
		$this->AddMap("Attributes", "attributes", "attributes", DATATYPE_TEXT, false);
		$this->AddMap("Validators", "validators", "validators", DATATYPE_TEXT, false);
		$this->AddMap("FormID", "form_id", "form_id", DATATYPE_TEXT, true);
		$this->AddMap("Name", "name", "name", DATATYPE_TEXT, true);
		$this->AddMap("SortOrder", "sortOrder", "sortOrder", DATATYPE_INTEGER, true);
	}
  
	function Retrieve(&$form)
	{
		$sql  = "SELECT ";
		$sql .= $this->GetFieldList();
		$sql .= "FROM " . $this->GetTableName();
		$sql .= $this->GetWhereClause($form->FormFields);
		$sql .= "ORDER BY sortOrder";

		$form->FormFields->Collection = array();

		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$form->FormFields->Collection[$row['name']] = $this->FillRow($row);
		}
	}
  
	function GetFieldList($select='', $prefix='tbl')
	{
		parent::GetFieldList(&$select, $prefix);
		
		return $select;
	}

	function FillRow($row, $formField=null)
	{
		if (!$formField)
		{
			$formField = new FormFieldModel();
		}
		
		parent::FillRow(&$formField, &$row);
		
		return $formField;
	}
}

Class FormEntryData extends db
{
	function FormEntryData()
	{
		parent::db();
		
		$this->tableName = 'formentry';

		$this->AddMap("ID", "id", "id", DATATYPE_INTEGER, true);
		$this->AddMap("FormID", "ticketCode", "ticketCode", DATATYPE_TEXT, true);
		$this->AddMap("FormFieldID", "formfield_id", "formfield_id", DATATYPE_INTEGER, true);
		$this->AddMap("Value", "value", "value", DATATYPE_TEXT, false);
	}
  
	function Retrieve(&$form)
	{
//print('<pre>'.var_export($form,true).'</pre>');//exit;
		$fieldData = new FormFieldData();
		$selectField = '';
		$fieldData->GetFieldList(&$selectField, 'fld');

		$sql  = "SELECT ";
		$sql .= $this->GetFieldList();
		$sql .= ", " . $selectField;
		$sql .= "FROM " . $this->GetTableName();
		$sql .= ", " . $this->GetTableName('formfield', 'fld', true);
		$sql .= $this->GetWhereClause($form->FormFields);
		$sql .= "AND tbl.formfield_id = fld.id ";
		$sql .= "ORDER BY sortOrder";

		$form->FormFields->Collection = array();

//print($sql);exit;
		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$field = $this->FillRow($row);
			$field = $fieldData->FillRow($row, $field);
			$form->FormFields->Collection[] = $field;
		}
	}
  
	function FillRow($row)
	{
		$formField = new FormFieldModel();
		
		parent::FillRow(&$formField, &$row);
		
		return $formField;
	}
	
	function GetFieldList()
	{
		parent::GetFieldList(&$select);
		
		return $select;
	}

	function Update(&$formEntry, $formID)
	{
		/*if (isset($formEntry->ID) && !empty($formEntry->ID) && $formEntry->ID != -1)
		{
			$sql  = "UPDATE " . $this->GetTableName(false);
			$sql .= $this->GetUpdateList($formEntry);
			$sql .= "WHERE id=" . $formEntry->ID;
		}
		else
		{*/
		  $formEntry->FormID = $formID;
		  $formEntry->FormFieldID = $formEntry->ID;

			$sql  = "INSERT INTO " . $this->GetTableName(false);
			$sql .= $this->GetInsertFieldList();
			$sql .= $this->GetInsertValueList(&$formEntry);
		/*}*/
//print('<pre>'.var_export($formEntry,true).'</pre>');//exit;
//print($sql.'<br />');//exit;

	    $rows = $this->Execute($sql);
	}
  
}
?>
